<?php
include '../library/Template.class.php';

class DrillRencanaPanen{
	private $tahun;
	private $bulan;
	private $no_individu;
	private $db;
	private $translator;
	private $month_config;
	
	function DrillRencanaPanen($db=NULL, $bulan=NULL, $tahun=NULL, $no_individu=NULL){
		$this->db   = $db;
		$this->translator = array(
			"Januari" 	=> '01',
			"Februari"	=> '02',
			"Maret"		=> '03',
			"April"		=> '04',
			"Mei"		=> '05',
			"Juni"		=> '06',
			"Juli"		=> '07',
			"Agustus"	=> '08',
			"September"	=> '09',
			"Oktober"	=> '10',
			"November"	=> '11',
			"Desember"	=> '12'
			);
		
		$this->bulan 	   = $bulan;		
		$this->tahun 	   = $tahun;		
		$this->no_individu = $no_individu;		
		
		$this->month_number = array(
			"Januari" 	=> 31,
			"Februari"	=> ($this->tahun % 4 == 0) ? 29 : 28,
			"Maret"		=> 31,
			"April"		=> 30,
			"Mei"		=> 31,
			"Juni"		=> 30,
			"Juli"		=> 31,
			"Agustus"	=> 31,
			"September"	=> 30,
			"Oktober"	=> 31,
			"November"	=> 30,
			"Desember"	=> 31,
			);
	}	
	
	private function createDrillSettingXml(){
		$tpl_path = '../include/chart_template/';
		$tpl_file = 'rencana_panen/drill_setting.xml.php';
		$out_path = '../include/chart_setting/';
		$out_file = 'rencana_panen_drill.xml';
		
		$tpl = new Template($tpl_path);
		
		$contents = array(
			'chart_type'	=> 'line',
			'chart_title'	=> 'Realisasi Panen ('.$this->bulan.' '.$this->tahun.')',
		);
		
		$tpl->set_vars($contents);
		
		file_put_contents($out_path.$out_file, $tpl->fetch($tpl_file));
	}
	
	private function createDrillDataXml(){
		$tpl_path = '../include/chart_template/';
		$tpl_file = 'rencana_panen/drill_data.xml.php';
		$out_path = '../include/chart_data/';
		$out_file = 'rencana_panen_drill.xml';
		
		$tpl = new Template($tpl_path);
		
		$graph_value  = array_fill(1,$this->month_number[$this->bulan],0);
		$graph_series = array_fill(1,$this->month_number[$this->bulan],0);
		$graph_series = array_combine( array_keys($graph_series), array_keys($graph_series));
		foreach($this->data as $d){
			$graph_value[$d['tanggal']] = $d['jumlah'];
		}
		
		$contents = array(
			'graph_value'  => $graph_value,
			'graph_series' => $graph_series
		);
		
		$tpl->set_vars($contents);
		
		file_put_contents($out_path.$out_file, $tpl->fetch($tpl_file));
	}
	
	private function getData(){
        $q = "
            SELECT DAY(tgl_panen) as tanggal, COUNT(tgl_panen) as jumlah 
            FROM `caramel_operational`.`panen_betina` 
            WHERE MONTH(`tgl_panen`)='".$this->translator[$this->bulan]."'
            AND YEAR(`tgl_panen`)='".$this->tahun."'
            GROUP BY tgl_panen";
		
		
		$r = $this->db->get_results($q, ARRAY_A);
		
		return $r;
	}
	
	private function printJsonData(){
		$graph_value = array_fill(1,31,0);
		
		foreach($this->data as $d){
			$graph_value[$d['tanggal']] = $d['jumlah'];
		}
		$graph_value[0] = $this->bulan;
		
		echo json_encode($graph_value);
	}

	public function response(){
		$this->data = $this->getData();
		
		if( $this->data > 0 ){
			// refresh setting
			$this->createDrillSettingXml();
			
			// refresh data
			$this->createDrillDataXml();
			
			// print data
			$this->printJsonData();
		}
		else{
			echo "false";
		}
	}	
}

$bulan 		 = $_GET['bulan'];
$tahun 		 = $_GET['tahun'];
$no_individu = $_GET['no_individu'];


// load database class handler
include_once '../config/db_config.php';
include '../library/ezsql/shared/ez_sql_core.php';
include '../library/ezsql/mysql/ez_sql_mysql.php';

// database handler
$db = new ezSQL_mysql($dbUser, $dbPass, $dbName, $dbHost);

$finder = new DrillRencanaPanen($db, $bulan, $tahun, $no_individu);
$finder->response();
?>